package org.example.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.mapper.RoleMapper;
import org.example.pojo.*;
import org.example.service.RoleService;
import org.example.service.UserService;
import org.example.util.JWTUtils;
import org.example.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
@Transactional
@Api("role测试")
public class RoleController {


    @Autowired
    RoleService roleService;


    public HttpServletRequest request;


    @PostMapping("/showRoles")
    @ResponseBody
    @ApiOperation("显示所有权限")
    public ResponseData showUsers(int page,int limit){

        //使用PageHelper开启分页功能
        PageHelper.startPage(page,limit);

        //紧跟着PageHelper.startPage(page,limit)的下一条查询语句会被拦截
        //添加分页实现,并且查询了此sql对应的count
        List<Role> roles = roleService.selectRoles();


        //根据查询出来的集合生成一个PageInfo对象
        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);

        System.out.println(roles);

        ResponseData jsonData = ResponseData.getJsonData(roles,(int)rolePageInfo.getTotal());

        return jsonData;
    }



    @PostMapping(value = "/showRes")
    @ResponseBody
    @ApiOperation("展示角色权限")
    public RoleRes showRes(Integer rId){


        return roleService.selectRoleRes(rId);

    }


    @PostMapping("/editRole")
    @ResponseBody
    @ApiOperation("修改角色")
    public ResponseData editRole(@RequestBody EditRole editRole){


        System.out.println(editRole);

        int rId = roleService.selectByName(editRole.getrName()).getrId();
        Role newRole = new Role();
        newRole.setrId(rId);
        newRole.setrName(editRole.getrName());
        newRole.setrDescription(editRole.getrDescription());

        roleService.updateRolePwd(newRole);

        roleService.deleteRes(rId);

        for (String resName:editRole.getRoles()) {

            ResId resId = roleService.selectResId(resName);
            int id = resId.getResId();
            roleService.insertRes(rId,id);

        }

        return ResponseData.getSccesss("修改成功",100);

    }

    @PostMapping("/addRole")
    @ResponseBody
    @ApiOperation("添加角色")
    public ResponseData addRole(@RequestBody EditRole editRole){


        System.out.println(editRole);


        roleService.addRole(editRole);

        int rId = roleService.selectByName(editRole.getrName()).getrId();

        for (String resName:editRole.getRoles()) {

            ResId resId = roleService.selectResId(resName);
            int id = resId.getResId();
            roleService.insertRes(rId,id);

        }

        return ResponseData.getSccesss("添加成功",100);

    }


    @GetMapping("/selectByAll/{page}/{limit}/{var}")
    @ResponseBody
    @ApiOperation("模糊查询角色")
    public ResponseData selectByAll(@ApiParam("当前页") @PathVariable int page, @ApiParam("每一页显示几条数据") @PathVariable int limit, @ApiParam("模糊查询输入的内容") @PathVariable String var) {


        PageHelper.startPage(page,limit);


        Map<String,String> map = new HashMap<>();

        List<Role> roles= roleService.selectVariablesByAll(var);
        //根据查询出来的集合生成一个PageInfo对象
        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);
        ResponseData responseData = ResponseData.getJsonData(roles,(int)rolePageInfo.getTotal());
        return responseData;

    }

    @PostMapping("/deleteUser")
    @ResponseBody
    @ApiOperation("删除角色")
    public ResponseData deleteUser(int rId){




        roleService.deleteRole(rId);


        return ResponseData.getSccesss("删除成功",100);

    }

}
